package com.niubility.controller;


import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.niubility.entity.Department;
import com.niubility.entity.Pwd;
import com.niubility.entity.SearchMain;
import com.niubility.entity.Users;

import com.niubility.exception.CustomException;
import com.niubility.server.IDepartmentService;
import com.niubility.server.IPwdService;
import com.niubility.server.IUserService;

import com.niubility.server.Impl.Aliyunapi;
import com.niubility.server.Impl.SearchServer;
import com.niubility.session.MySessionContext;
import com.niubility.vo.MySession;
import javafx.application.Application;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;


@Transactional
@RestController
public class UsersController {

    @Autowired
    private IUserService usersService;
    @Autowired
    private IPwdService pwdService;
    @Autowired
    private IDepartmentService departmentService;

    @Autowired
    private SearchServer searchServer;

    private Logger logger = Logger.getLogger(UsersController.class);
    /*
    * @Author: 亢凯永
    * @Date: 2018/2/2 23:55
    * @Dis: 用户注册
    * @Param:点击注册确定时调用
    * @Return:返回true为注册成功
    */
    @RequestMapping("/userRegisterHandler")
    public void insertUser(HttpServletResponse resp, HttpServletRequest req) throws CustomException {
        //设置字符集
        resp.setCharacterEncoding("utf-8");
        try {
            req.setCharacterEncoding("utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
            //拿到输入的数据
            String callback=req.getParameter("callback");
            String inputcode =  req.getParameter("inputcode");
            //System.out.println(inputcode);
            //创建session上下文，并通过sessionId拿到session对象
            MySessionContext myContext= MySessionContext.getInstance();
            HttpSession session = myContext.getSession(req.getParameter("sessionId"));
            //System.out.println(session.toString());
            //通过sessionId拿到验证码并进行比对
            String code =  (String)session.getAttribute("code");
            boolean judgecode = code.equals(inputcode)?true:false;
            //拿到提交的账号密码和验证码
            String userTel = req.getParameter("userTel");
            String userPwd = req.getParameter("userPwd");
            Users user = null;
            Pwd pwd  =null;
            String result = null;
            boolean bool1 = false;//向用户表插入数据
            boolean bool2 = false;//向密码表插入数据
            boolean bool =false;//用来判断是否成功
            String userpassword = null;
            if(judgecode==true) {
                //把用户的注册时间更新
                user = new Users();
                user.setUserTel(userTel);
                DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                user.setUserDate(df.format(new Date()));
                pwd = new Pwd();
                //把用户输入的密码进行加密处理
                userpassword = pwdService.CipherEncryptionBySHA1(userPwd, userTel);
                pwd.setUserPwd(userpassword);
            }
            /*
                向用户表和密码表插入数据，若都为true,则注册成功
             */
            SearchMain searchMain = new SearchMain();
            try {
                bool1 = usersService.Save(user);
                int  userId =  usersService.selectUserByTel(userTel).getUserId();
                pwd.setUserId(userId);
                bool2 = pwdService.Save(pwd);
                bool = bool1&bool2;
                searchMain.setId(userId);
                searchMain.setMaix(user.getUserName()+" "+user.getUserSign());
                result=callback+"({"+"\"sussess\":"+bool+"})";
            }catch (Exception e){
                try {
                    result = callback+"({"+"\"sussess\":"+bool+"})";
                    resp.getWriter().write(result);
                } catch (IOException e1) {
                    logger.debug("数据传输错误."+e);
                }
                logger.debug("操作失败."+e);
                throw new CustomException();
            }
            try {
                resp.getWriter().write(result);
                //跳至登陆页面
            } catch (Exception e) {
                e.printStackTrace();
                logger.debug("数据传输错误."+e);
            }
        }










    /*
    * @Author: 亢凯永
    * @Date: 2018/2/5 12:04
    * @Dis: 返回验证码的输入是否正确
    * @Param:验证码和sessionId
    * @Return:验证码输入正确
    */
    @RequestMapping("/judgeVerificationCode")
    public void judgeVerificationCode(HttpServletRequest req, HttpServletResponse resp){
            resp.setCharacterEncoding("utf-8");
        try {
            req.setCharacterEncoding("utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
            //拿到session里面的验证码并与用户输入的进行对比
            String callback=req.getParameter("callback");
            String inputcode =  req.getParameter("inputcode");
            String sessionId = req.getParameter("sessionId");
            MySessionContext myContext= MySessionContext.getInstance();
            HttpSession session = myContext.getSession(sessionId);
            String code =  (String)session.getAttribute("code");
            boolean bool  = false;
            String result = null;
            bool = code.equals(inputcode)?true:false;
            //System.out.println(bool);
            result=callback+"({"+"\"judgeCode\":"+bool+"})";
        try {
            resp.getWriter().write(result);
        } catch (IOException e) {
            e.printStackTrace();
            //System.out.println("验证码不正确");
        }
    }


    /*
    * @Author: 亢凯永
    * @Date: 2018/2/2 23:56
    * @Dis: 判断手机号是否存在于数据库
    * @Param:12位之内的手机号，传入为String  AJAX请求
    * @Return:返回一个对象，若对象不为空，则存在，此时不能注册
    */
    @RequestMapping("/judgeUserTel")
    public void judgeTel(HttpServletRequest req, HttpServletResponse resp) throws CustomException {
            resp.setCharacterEncoding("utf-8");
        try {
            req.setCharacterEncoding("utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
            String callback = req.getParameter("callback");
            boolean bool1 = false;//手机号存在于数据库 false为不存在 true表示存在
            String result = null; //用于返回结果
            String userTel = req.getParameter("userTel");
            boolean bool = false;
        try {
            //通过正则判断手机号格式是否正确
            bool =  usersService.isChinaPhoneLegal(userTel);
            if(bool==true) {
                //System.out.println(userTel);
                Users user = usersService.selectUserByTel(userTel);
                if(user!=null) {
                    bool1 = true;
                }
            }else{
                bool1 = false;
                //System.out.println("电话号码不正确");
            }
            result = callback + "({" + "\"boolfsaf\":" + bool1 + "})";
            //System.out.println(bool1);
            resp.getWriter().write(result);
        } catch (Exception e) {
            bool1 = false;
            result = callback + "({" + "\"boolfsaf\":" + bool1 + "})";
            try {
                resp.getWriter().write(result);
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            logger.debug("输出异常！"+e);
            //System.out.println("手机号码获取出错"+e.getMessage());
            throw new CustomException();
        }
    }

    /*
    * @Author: 亢凯永
    * @Date: 2018/2/4 1:04
    * @Dis: 判断邮箱返回true可以注册(此处现留接口，但目前用不到)
    * @Param:邮箱账号
    * @Return:返回此用户，若不存在返回null
    */
    @RequestMapping("/judgeUserEmail")
    public void judgeEmail(HttpServletRequest req, HttpServletResponse resp) throws CustomException {
        //设置字符集
        resp.setCharacterEncoding("utf-8");
        try {
            req.setCharacterEncoding("utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        //邮箱验证
        String callback = req.getParameter("callback");
        String userEmail = req.getParameter("userEmail");
        try {
            Users user =  usersService.selectUserByEmail(userEmail);
            String result = callback+"("+JSONObject.fromObject(user).toString()+")";
            resp.getWriter().write(result);
        } catch (IOException e) {
            String result = callback+"({"+"\"returncode\":"+false+"})";
            try {
                resp.getWriter().write(result);
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            //System.out.println("邮箱号码出现异常"+e.getMessage());
            throw new CustomException();
        }catch (Exception e){
            e.printStackTrace();
            //System.out.println("邮箱获取出错"+e.getMessage());
        }
    }

    /*
    * @Author: 亢凯永
    * @Date: 2018/2/3 0:03
    * @Dis: 发送验证码
    * @Param:一个点击事件
    * @Return:6位的数字验证码
    */
    @RequestMapping("/postVerification")
    public void postVerification(HttpServletRequest req, HttpServletResponse resp) throws CustomException {
            resp.setCharacterEncoding("utf-8");
            try {
                req.setCharacterEncoding("utf-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            String userTel =  req.getParameter("userTel");
            //System.out.println("----------------->"+userTel);
            String callback = req.getParameter("callback");
            int randomCode;
            Random rm = new Random();
            randomCode = rm.nextInt(900000)+100000-1;
            String num =  String.valueOf(randomCode);
            MySession mySession = new MySession();
            SendSmsResponse response = null;
            String sessionId = null;
            HttpSession sess = null;
            String lis = null;
            String str = null;
        try {
            response = Aliyunapi.sendSms(userTel,num);
        } catch (com.aliyuncs.exceptions.ClientException e) {
            lis = callback +"({"+"\"boolfsaf\":"+"\""+false+"\""+"})";
            try {
                resp.getWriter().write(lis);
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            //System.out.println("阿里云验证码发送异常"+e.getMessage());
            throw new CustomException();
        }
        if("OK".equals(response.getCode())) {
            //System.out.println("在OK中");
            sessionId = req.getSession().getId();
            req.getSession().setAttribute("code",num);
            mySession.setSessionId(sessionId);

            ////System.out.println("sessionId---------------------->:"+sessionId);
            lis = callback+"("+ JSONObject.fromObject(mySession).toString()+")";
        }else{
             lis = callback +"({"+"\"boolfsaf\":"+"\""+false+"\""+"})";
             //System.out.println(lis);
        }
        try {
            resp.getWriter().write(lis);
        } catch (IOException e) {
            e.printStackTrace();
            //System.out.println("输出错误:"+e.getMessage());
        }
    }


    /*
    * @Author: 亢凯永
    * @Date: 2018/2/3 0:03
    * @Dis: 查询用户信息
    * @Param: 在管理员身份下点击事件
    * @Return: 返回用户信息
    */
    @RequestMapping("/loadUserAll")
    public void loadAllUser(HttpServletRequest req,HttpServletResponse resp) throws CustomException {
        resp.setCharacterEncoding("utf-8");
        try {
            req.setCharacterEncoding("utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String callback=req.getParameter("callback");
        int page=Integer.parseInt(req.getParameter("page"));
        int displayCount=Integer.parseInt(req.getParameter("displayCount"));
        int maxpage=usersService.LoadCount()/displayCount;
        int turn=Integer.parseInt(req.getParameter("turn"));
        page+=turn;
        page=page>maxpage?0:page<0?maxpage:page; //对翻页进行判断
        page=page*maxpage;
        try {
            List<Users> list= usersService.LoadAll(page,displayCount);
            String result=callback+"("+ JSONArray.fromObject(list).toString()+")";
            resp.getWriter().write(result);
        } catch (Exception e) {
            e.printStackTrace();
            //System.out.println("查询用户信息出错:"+e.getMessage());
            throw new CustomException();
        }
    }


    /*
     * @Author: 亢凯永
     * @Date: 2018/2/4 1:14
     * @Dis: 用户信息修改(不包括密码)
     * @Param:用户信息封装的用户对象，userName不能为空，userAge不可以为空，userEmial不能为空，默认未绑定邮箱
     * @Return:true表示修改成功  false表示修改失败
     */
    //用户信息修改
    @RequestMapping("/userUpdateHandler")
    @ResponseBody
    public void updateuser(HttpServletRequest req, HttpServletResponse resp){
        String callback=req.getParameter("callback");
        resp.setCharacterEncoding("utf-8");
        try {
            req.setCharacterEncoding("utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
         int userId = Integer.parseInt(req.getSession().getAttribute("userId").toString());
        Users user = new Users();
        user.setUserId(userId);
        user.setUserAge(Integer.parseInt(req.getParameter("userAge")));
        //System.out.println(Integer.parseInt(req.getParameter("userAge")));
        String userName = req.getParameter("userName");
        user.setUserName(userName);
        user.setUserSex(req.getParameter("userSex"));
        user.setUserEmail(req.getParameter("userEmail"));
        user.setUserLocation(req.getParameter("userLocation"));
        String userSign = req.getParameter("userSign");
        user.setUserSign(userSign);
        user.setUserPicture(req.getParameter("userPicture"));
         SearchMain searchMain = new SearchMain();
         searchMain.setId(userId);
         searchMain.setMaix(userName+" "+userSign);
        try {
            searchServer.Update(searchMain);
        } catch (Exception e) {
            logger.debug("检索表存储失败！");
        }
        //System.out.println(user.toString());
        try {
            boolean bool = usersService.Update(user);
            String lis = callback +"({"+"\"boolfsaf\":"+bool+"})";
            resp.getWriter().write(lis);
        } catch (IOException e) {
            logger.debug("数据写入失败!");
        }
    }


    /*
    * @Author: 亢凯永
    * @Date: 2018/2/4 1:20
    * @Dis:  用户登陆后点我的查询信息
    * @Param: 传入用户的userId
    * @Return:返回用户的对象信息
    */

    @RequestMapping("/loadUserById")
    @ResponseBody
    public void loadUserById(HttpServletRequest req, HttpServletResponse resp) throws CustomException {
        resp.setCharacterEncoding("utf-8");
        try {
            req.setCharacterEncoding("utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String callback=req.getParameter("callback");
        int userId = Integer.parseInt(req.getSession().getAttribute("userId").toString());
//        //System.out.println(userId);
        Users user = null;
        try{
            user =  usersService.LoadById(userId);
        }catch (Exception e){
            String lis = callback +"({"+"\"boolfsaf\":"+false+"})";
            try {
                resp.getWriter().write(lis);
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            throw new CustomException();
        }
//        //System.out.println(user);
        String result=callback+"("+ JSONArray.fromObject(user).toString()+")";
        try {
            resp.getWriter().write(result);
        } catch (IOException e) {
            e.printStackTrace();
//            //System.out.println("查询用户信息出错:"+e.getMessage());
        }
    }

    /*
     * @Author: 亢凯永
     * @Date: 2018/2/6 21:50
     * @Dis: 社团登陆
     * @Param: 输入社团的depAcount和password
     * @Return:返回登陆是否成功   true登陆成功   false登陆失败
     */
    @RequestMapping("/depLogin")
    @ResponseBody
    public void userLoginTest(HttpServletRequest req, HttpServletResponse resp){
        String result = null;
        resp.setCharacterEncoding("utf-8");
        try {
            req.setCharacterEncoding("utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String callback=req.getParameter("callback");
        boolean bool = false;
        String depAcount = req.getParameter("depAcount");
        String userPassword = req.getParameter("userPwd");
        Department department = departmentService.loadDepByAcount(depAcount);
        if(department!=null&&department.getDepId()>=200000000&&department.getDepId()<300000000){
            String userPwd1 = pwdService.CipherEncryptionBySHA1(userPassword,depAcount);
            String userPwd  = pwdService.LoadById(department.getDepId()).getUserPwd();
            Pwd pwd = new Pwd();
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            pwd.setUserLastLoginDate(df.format(new Date()));
            pwd.setUserId(department.getDepId());
            if(userPwd1.equals(userPwd)){
                pwd.setUserStatus("1");
                pwdService.Update(pwd);
//                //System.out.println("登陆成功");
                bool  = true;
            }
            }else{
                bool = false;
//                //System.out.println("账号或密码输入错误");
            }
            result = callback + "({" + "\"loginStatus\":" + bool + "})";
            try {
                resp.getWriter().write(result);
            } catch (IOException e) {
                e.printStackTrace();
            }
    }

    /*
     * @Author: 亢凯永
     * @Date: 2018/2/6 21:50
     * @Dis: 社团登陆
     * @Param: 输入社团的depAcount和password
     * @Return:返回登陆是否成功   true登陆成功   false登陆失败
     */
    @RequestMapping("/deptLogin")
    public void depLogin(HttpServletRequest req, HttpServletResponse resp) throws CustomException {
        String result = null;
        resp.setCharacterEncoding("utf-8");
        try {
            req.setCharacterEncoding("utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String callback=req.getParameter("callback");
        //拿到提交的账号密码和验证码
        String depAcount = req.getParameter("depAcount");
        String userPwd = req.getParameter("userPwd");
        try{
            Subject currrentUser = SecurityUtils.getSubject();
            boolean bool = false;
            if(!currrentUser.isAuthenticated()) {
                //把用户名和密码封装为UsernamePasswordToken对象
                UsernamePasswordToken token = new UsernamePasswordToken(depAcount, userPwd);
                token.setRememberMe(true);
                try {
                        Department department =  departmentService.loadDepByAcount(depAcount);
                        int userId = department.getDepId();
                        Pwd pwd =  pwdService.LoadById(userId);
                        if(pwd.getUserStatus().equals("1")) {
                            throw new CustomException();
                        }
                        else if(pwd.getUserStatus().equals("0")){
                            //执行登陆
                            currrentUser.login(token);
//                            //System.out.println("登陆成功");
                            //拿到userId放入session，并在此时更新用户的最后一次登录时间
                            currrentUser.logout();
//                            //System.out.println("----------"+pwd.getUserStatus());
                            pwd.setUserStatus("1");
                            DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                            pwd.setUserLastLoginDate(df.format(new Date()));
                            pwdService.Update(pwd);
                            req.getSession().setAttribute("userId",userId);
//                            //System.out.println("========"+pwd.getUserStatus());
                        }
//                            //System.out.println("----------------->"+pwd.getUserStatus());
                            bool = true;
                        } catch (AuthenticationException ae) {
                            ae.printStackTrace();
                            //System.out.println("登陆失败:" + ae.getMessage());
                            bool = false;
                        }
                    }
                    result = callback + "({" + "\"loginStatus\":" + bool + "})";
                    }catch (Exception e){
                    result = callback + "({" + "\"loginStatus\":" + false + "})";
                    try {
                        resp.getWriter().write(result);
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                    throw new CustomException();
                }
                    try {
                    resp.getWriter().write(result);
                } catch (IOException e) {
                    e.printStackTrace();
                    //System.out.println("传输数据失败:"+e.getMessage());
                }
}
    /*
    * @Author: 亢凯永
    * @Date: 2018/2/4 1:21
    * @Dis: 实现用户登陆，但还没有实现单点登陆，
    * @Param: 用户的userTel 和用户的userPwd
    * @Return: 返回是否登录成功
    */

    @RequestMapping("/userLogin")
    @ResponseBody
    public void userLogin(HttpServletRequest req, HttpServletResponse resp) throws CustomException {
        String result = null;
        resp.setCharacterEncoding("utf-8");
        try {
            req.setCharacterEncoding("utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String callback=req.getParameter("callback");
        //拿到提交的账号密码和验证码
        String userTel = req.getParameter("userTel");
        //System.out.println(userTel);
        String userPwd = req.getParameter("userPwd");
        //System.out.println(userPwd);
        try{
            Subject currrentUser = SecurityUtils.getSubject();
            boolean bool = false;
            if(!currrentUser.isAuthenticated()) {
                //把用户名和密码封装为UsernamePasswordToken对象
                UsernamePasswordToken token = new UsernamePasswordToken(userTel, userPwd);
                //System.out.println(token.toString());
                token.setRememberMe(true);//记住我功能
                try {
                    Users user =  usersService.selectUserByTel(userTel);
                    int userId  = user.getUserId();
                    Pwd pwd  = pwdService.LoadById(userId);
                    if(pwd.getUserStatus().equals("1")) {
                        throw new CustomException();
                    }
                    else {
                        //执行登陆
                        currrentUser.login(token);
//                        //System.out.println("登陆成功");
                        //拿到userId放入session，并在此时更新用户的最后一次登录时间
                        currrentUser.logout();
                        //System.out.println("----------"+pwd.getUserStatus());
                        pwd.setUserStatus("1");
                        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                        pwd.setUserLastLoginDate(df.format(new Date()));
                        pwdService.Update(pwd);
                        req.getSession().setAttribute("userId",userId);
//                        //System.out.println("========"+pwd.getUserStatus());
                    }
//                    //System.out.println("----------------->"+pwd.getUserStatus());
                    bool = true;
                } catch (AuthenticationException ae) {
                    ae.printStackTrace();
                    //System.out.println("登陆失败:" + ae.getMessage());
                    bool = false;
                }
            }
            result = callback + "({" + "\"loginStatus\":" + bool + "})";
        }catch (Exception e){
            result = callback + "({" + "\"loginStatus\":" + false + "})";
            try {
                resp.getWriter().write(result);
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            throw new CustomException();
        }
        try {
            resp.getWriter().write(result);
        } catch (IOException e) {
            e.printStackTrace();
            //System.out.println("传输数据失败:"+e.getMessage());
        }
    }







    /*
    * @Author: 亢凯永
    * @Date: 2018/2/5 16:02
    * @Dis: 登出  用户在点击登出按钮时触发
    * @Param: 点击事件
    * @Return: 返回登出是否成功
    */
    @RequestMapping("/logout")
    public void userLogout(HttpServletRequest req, HttpServletResponse resp){
        String result = null;
        resp.setCharacterEncoding("utf-8");
        try {
            req.setCharacterEncoding("utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        boolean bool = true;
        String callback=req.getParameter("callback");
        Subject currrentUser = SecurityUtils.getSubject();
        try {
            int userId = Integer.parseInt(currrentUser.getSession().getAttribute("userId").toString());
            Pwd pwd = pwdService.LoadById(userId);
            pwd.setUserStatus("0");
            pwdService.Update(pwd);
            currrentUser.logout();
        }catch(Exception e){
            bool = false;
            e.printStackTrace();
            //System.out.println("未登录不能登出"+e.getMessage());
        }
        result = callback + "({" + "\"logout\":" + bool + "})";
        try {
            resp.getWriter().write(result);
        } catch (IOException e) {
            e.printStackTrace();
            //System.out.println("传输数据失败:"+e.getMessage());
        }
    }


    /*
    * @Author: 亢凯永
    * @Date: 2018/2/4 19:02
    * @Dis: 用户重置密码时为了拿到userTel ,放入session
    * @Param: 用户的usertel
    * @Return:
    */
    @RequestMapping("getUserTel")
    public void getUserTel(HttpServletRequest req, HttpServletResponse resp){
        resp.setCharacterEncoding("utf-8");
        try {
            req.setCharacterEncoding("utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String userTel = req.getParameter("userTel");
        req.getSession().setAttribute("userTel",userTel);
    }


    /*
    * @Author: 亢凯永
    * @Date: 2018/2/4 18:21
    * @Dis: 密码重置
    * @Param:    点击事件ajax 请求发送六位手机验证码，验证通过，跳至修改密码界面
    * @Return: 返回true为修改成功 false修改失败
    */
    @RequestMapping("/resetPwd")
    public void UserTelCode(HttpServletRequest req, HttpServletResponse resp) throws CustomException {
        resp.setCharacterEncoding("utf-8");
        try {
            req.setCharacterEncoding("utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String callback=req.getParameter("callback");
        String inputcode =  req.getParameter("inputcode");
        MySessionContext myContext= MySessionContext.getInstance();
        HttpSession session = myContext.getSession(req.getParameter("sessionId"));
        String code =  (String)session.getAttribute("code");
        boolean judgecode = code.equals(inputcode)?true:false;
        //拿到提交的账号密码和验证码
        String userTel = req.getParameter("userTel");
        String userPassword = req.getParameter("userPwd");
        String result = null;
        Boolean bool = false;
        try{

            String userPwd  = pwdService.CipherEncryptionBySHA1(userPassword,userTel);
            Users user =  usersService.selectUserByTel(userTel);
            Pwd pwd = new Pwd();
            if(user!=null&&user.getUserId()>=100000000) {
                pwd.setUserPwd(userPwd);
                pwd.setUserId(user.getUserId());
                bool =  pwdService.Update(pwd);
            }
            result = callback +"({"+"\"boolfsaf\":"+bool+"})";
        }catch (Exception e){
            result = callback +"({"+"\"boolfsaf\":"+bool+"})";
            try {
                resp.getWriter().write(result);
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            throw new CustomException();
        }
        try{
            resp.getWriter().write(result);
        } catch (IOException e) {
            e.printStackTrace();
        }catch (Exception e){
           e.printStackTrace();
        }
    }
}
